
package coupons

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/coupons"
    couponsReq "github.com/flipped-aurora/gin-vue-admin/server/model/coupons/request"
)

type CouponsService struct {}
// CreateCoupons 创建coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService) CreateCoupons(coupon *coupons.Coupons) (err error) {
	err = global.GVA_DB.Create(coupon).Error
	return err
}

// DeleteCoupons 删除coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService)DeleteCoupons(id string) (err error) {
	err = global.GVA_DB.Delete(&coupons.Coupons{},"id = ?",id).Error
	return err
}

// DeleteCouponsByIds 批量删除coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService)DeleteCouponsByIds(ids []string) (err error) {
	err = global.GVA_DB.Delete(&[]coupons.Coupons{},"id in ?",ids).Error
	return err
}

// UpdateCoupons 更新coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService)UpdateCoupons(coupon coupons.Coupons) (err error) {
	err = global.GVA_DB.Model(&coupons.Coupons{}).Where("id = ?",coupon.Id).Updates(&coupon).Error
	return err
}

// GetCoupons 根据id获取coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService)GetCoupons(id string) (coupon coupons.Coupons, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&coupon).Error
	return
}
// GetCouponsInfoList 分页获取coupons表记录
// Author [yourname](https://github.com/yourname)
func (couponService *CouponsService)GetCouponsInfoList(info couponsReq.CouponsSearch) (list []coupons.Coupons, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&coupons.Coupons{})
    var coupons []coupons.Coupons
    // 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&coupons).Error
	return  coupons, total, err
}
func (couponService *CouponsService)GetCouponsPublic() {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
